package Models;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;

public class Sale {
    final NumberFormat money = new DecimalFormat("$#0.00");
    
    private int id;// Sales.SId;
    private Customer customerId;// Sales.CustID;
    private DiscountCode discountCodeId;// Sales.SDisc;
    private String notes;// Sales.SNotes;
    private java.util.Date date;// Sales.SDate;
    private FGI salesFGIID;
    private int salesFGIQty;
    private int kegID;
    private double priceOverride;
    private double servingTankSaleVolume;
    private List<LegacySales> legacySales;
    private double unitPrice;
    
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public Customer getCustomerId() {
		return customerId;
	}
	public void setCustomerId(Customer customerId) {
		this.customerId = customerId;
	}
	public DiscountCode getDiscountCodeId() {
		return discountCodeId;
	}
	public void setDiscountCodeId(DiscountCode discountCodeId) {
		this.discountCodeId = discountCodeId;
	}
	public String getNotes() {
		return notes;
	}
	public void setNotes(String notes) {
		this.notes = notes;
	}
	public java.util.Date getDate() {
		return date;
	}
	public void setDate(java.util.Date date) {
		this.date = date;
	}
	public FGI getSalesFGIID() {
		return salesFGIID;
	}
	public void setSalesFGIID(FGI salesFGIID) {
		this.salesFGIID = salesFGIID;
	}
	public int getSalesFGIQty() {
		return salesFGIQty;
	}
	public void setSalesFGIQty(int salesFGIQty) {
		this.salesFGIQty = salesFGIQty;
	}
	public int getKegID() {
		return kegID;
	}
	public void setKegID(int kegID) {
		this.kegID = kegID;
	}
	public double getPriceOverride() {
		return priceOverride;
	}
	public void setPriceOverride(double priceOverride) {
		this.priceOverride = priceOverride;
	}
	public double getServingTankSaleVolume() {
		return servingTankSaleVolume;
	}
	public void setServingTankSaleVolume(double servingTankSaleVolume) {
		this.servingTankSaleVolume = servingTankSaleVolume;
	}
	public List<LegacySales> getLegacySales() {
		return legacySales;
	}
	public void setLegacySales(List<LegacySales> legacySales) {
		this.legacySales = legacySales;
	}
	public double getUnitPrice() {
		return unitPrice;
	}
	public void setUnitPrice(double unitPrice) {
		this.unitPrice = unitPrice;
	}
	public NumberFormat getMoney() {
		return money;
	}

    /***********************************************************************
     * Standard display for pricing and discount text, etc.
     **********************************************************************/
    /***********************************************************************
     * Get Line Price (returns line item price based on settings of line)
     * @return Price
     **********************************************************************/
    public double getLinePrice(){
        double price = this.getUnitPrice();
        if(this.getPriceOverride()>0){
            price = this.getPriceOverride();
        }
        if(this.getDiscountCodeId()!=null){
            price = price - (price * this.getDiscountCodeId().getDiscNum());
        } 
        return price;
    }

    /***************************************************************************
     * Get Sales Line's subtotal based on settings.
     * @return 
     **************************************************************************/
    public double getLineSubTot(){
        final double subTot = getLinePrice() * (double)this.getSalesFGIQty();
        return subTot;
    }

    /***************************************************************************
     * Get the appropriate discount description based on settings.
     * @return 
     **************************************************************************/
    public String getDiscountText(){
        if(this.getPriceOverride()>0){
            return "Price Override";
        } else if (this.getDiscountCodeId()!=null) {
            return this.getDiscountCodeId().getDescription();
        } else {
            return "No Discount";
        }
    }

    /***************************************************************************
     * Get String in CSV format for displaying sales line information.
     * @return 
     **************************************************************************/
    public String getSalesString(final Administration admin){
        String str = "";
        final int numUnits = this.getSalesFGIQty();
        str += this.getSalesFGIID().getDisplayString();
        if(this.getKegID()!=0){
            str += " - " + String.format(admin.getKegPrefix()+"%08d", this.getKegID());
        }
        str +="," + money.format(getLinePrice());
        str +="," + Integer.toString(numUnits);
        str +="," + money.format(getLineSubTot()) + "\n";
        return str;
    }
}
